<?php

namespace App\Http\Controllers\Api;

use App\FlashSale;
use App\Goods;
use App\SpecGoodsPrice;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FlashSaleController extends BaseController
{
    protected $promotionType = 'App\FlashSale';
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $orderBy = $request->order_by ? $request->order_by : 'id';
        $orderType = $request->order_type ? $request->order_type : 'desc';
        $pageSize = $request->page_size ? $request->page_size : $this->pageSize;
        $lists = FlashSale::orderBy($orderBy, $orderType)->with(['goods.specPrices']);
        return $lists->paginate($pageSize);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $item = $item = FlashSale::where('goods_id', $request->goods_id)->ofActive()->first();
        if ($item) {
            return response(['message' => '您所选择的商品已参与活动，请选择其他商品或其他规格'], 400);
        }
        FlashSale::create($request->all());
        return response(['message' => '创建成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\FlashSale  $flashSale
     * @return \Illuminate\Http\Response
     */
    public function show(FlashSale $flashSale)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\FlashSale  $flashSale
     * @return \Illuminate\Http\Response
     */
    public function edit(FlashSale $flashSale)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\FlashSale  $flashSale
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, FlashSale $flashSale)
    {
        // 检查是否有订单
        if ($flashSale->sales_count > 0) {
            return response(['message' => '该活动已有订单，不能编辑']);
        }
        // 更改商品时
        if ($request->goods_id != $flashSale->goods_id) {
            $item = FlashSale::where('goods_id', $request->goods_id)->ofActive()->first();
            if ($item) {
                return response(['message' => '您所选择的商品已参与活动，请选择其他商品或其他规格'], 400);
            }
        }
        $flashSale->update($request->all());
        return response(['message' => '修改成功']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\FlashSale  $flashSale
     * @return \Illuminate\Http\Response
     */
    public function destroy(FlashSale $flashSale)
    {
        $flashSale->delete();
        return response(['message' => '删除成功']);
    }
}
